﻿//
//                                                                                 (C) SocketCoder.Com 
// Note: This Project is just some simple samples, if you are looking for a production solution Visit 
// SocketCoder.Com to Buy The Full WCS Web Conferencing Solution Source Code. 
//
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace VoiceEncoder
{

    public class MemoryAudioSink : AudioSink
    {
        public event EventHandler OnBufferFulfill;

        AudioFormat _format;
        SpeexEncoderDecoder _speexencoder = new SpeexEncoderDecoder();

        private int _VoiceQuality = 6;
        public int VoiceQuality
        {
            set
            {
                _VoiceQuality = this._speexencoder.VoiceQuality = value;
            }
            get
            {
                return _VoiceQuality;
            }
        }

        protected override void OnCaptureStarted(){
            _format = this.CaptureSource.AudioCaptureDevice.DesiredFormat;
        }

        protected override void OnCaptureStopped(){}

        protected override void OnFormatChange(AudioFormat Format){}

        protected override void OnSamples(long sampleTime, long sampleDuration, byte[] sampleData)
        {

            if (OnBufferFulfill != null & sampleData.Length >= _format.SamplesPerSecond)
                {
                    byte[] EncodedBuffer = _speexencoder.Encode(sampleData);
                    OnBufferFulfill(EncodedBuffer, null);
                }
        }

    }
}
